package com.lhgray.sglhome.presentation.ui.compose

import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemColors
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.freightoa.foundation.app.MMKVUtils
import com.freightoa.foundation.app.MMKVkEY
import com.lhgray.sglhome.R
import com.lhgray.sglhome.presentation.viewModel.HomeState
import com.lhgray.sglhome.presentation.viewModel.HomeViewModel

@Composable
fun HomePage(viewModel: HomeViewModel, startZxing: () -> Unit) {
    val state by viewModel.uiState.collectAsStateWithLifecycle()
    HomeViewPage(state, viewModel, startZxing)
}


@Preview(showBackground = true)
@Composable
fun HomePreviewView() {
    //HomePage()
}

@Composable
private fun HomeViewPage(state: HomeState, viewModel: HomeViewModel, startZxing: () -> Unit) {
    var currentIndex by remember { mutableIntStateOf(0) }
    val navController = rememberNavController()
    val userType = MMKVUtils.getString(MMKVkEY.userType, "")
    val items = mutableListOf(
        BottomNavItem.Upload,
        BottomNavItem.Warehouse,
        BottomNavItem.Trucking,
        BottomNavItem.Account
    )
    if ("U" != userType) {
        items.remove(BottomNavItem.Warehouse)
    }

    Scaffold(
        bottomBar = {
            NavigationBar( Modifier.navigationBarsPadding().height(55.dp).defaultMinSize(65.dp) ){
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentRoute = navBackStackEntry?.destination?.route
                items.forEachIndexed { index, bottomNavItem ->
                    NavigationBarItem(
                        selected = currentRoute == bottomNavItem.route,
                        onClick = {
                            navController.navigate(bottomNavItem.route) {
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                            currentIndex = index
                        },
                        icon = {
                            if (currentRoute == bottomNavItem.route) {
                                bottomNavItem.selectedIcon()
                            } else {
                                bottomNavItem.noSelectedIcon()
                            }
                        },
                        label = { Text(bottomNavItem.label) },
                        alwaysShowLabel = false,
                        colors = customNavigationBarItemColors()
                    )
                }
            }
        }
    ) { paddingValues ->
        NavHost(
            navController = navController,
            startDestination = BottomNavItem.Upload.route,
            modifier = Modifier.padding(paddingValues)
        ) {
            composable(BottomNavItem.Upload.route) {
                PicUploadPage(viewModel, startZxing)
            }
            composable(BottomNavItem.Warehouse.route) {
                WarehousePage(viewModel, startZxing)
            }
            composable(BottomNavItem.Trucking.route) {
                TruckingPage(viewModel, startZxing)
            }
            composable(BottomNavItem.Account.route) {
                MyAccountPage(viewModel, startZxing)
            }
        }

    }
}
//// 底部导航项数据


sealed class BottomNavItem(
    val route: String,
    var selectedIcon: @Composable () -> Unit,
    var noSelectedIcon: @Composable () -> Unit,
    val label: String
) {

    data object Upload : BottomNavItem(
        route = "pic_upload",
        selectedIcon = {
            Icon(
                painterResource(R.drawable.house_select),
                contentDescription = null
            )
        },
        noSelectedIcon = {
            Icon(
                painterResource(R.drawable.house_no_select),
                contentDescription = null
            )
        },
        label = "Pic Upload"
    )

    data object Warehouse : BottomNavItem(
        route = "warehouse",
        selectedIcon = {
            Icon(
                painterResource(R.drawable.warehouse_select),
                contentDescription = null
            )
        },
        noSelectedIcon = {
            Icon(
                painterResource(R.drawable.warehouse_no_select),
                contentDescription = null
            )
        },
        label = "Warehouse"
    )

    data object Trucking : BottomNavItem(
        route = "trucking",
        selectedIcon = {
            Icon(
                painterResource(R.drawable.trucking_select),
                contentDescription = null
            )
        },
        noSelectedIcon = {
            Icon(
                painterResource(R.drawable.trucking_no_select),
                contentDescription = null
            )
        },
        label = "Trucking"
    )

    data object Account : BottomNavItem(
        route = "my_account",
        selectedIcon = {
            Icon(
                painterResource(R.drawable.my_sectel),
                contentDescription = null
            )
        },
        noSelectedIcon = {
            Icon(
                painterResource(R.drawable.my_no_sectel),
                contentDescription = null
            )
        },
        label = "My Account"
    )
}

@Composable
private fun customNavigationBarItemColors(): NavigationBarItemColors {
    return NavigationBarItemDefaults.colors(
        selectedIconColor = Color.Blue, // 选中时图标颜色
        unselectedIconColor = Color.Gray, // 未选中时图标颜色
        selectedTextColor = MaterialTheme.colorScheme.primary, // 选中时文字颜色
        unselectedTextColor = Color.Gray, // 未选中时文字颜色
        indicatorColor = MaterialTheme.colorScheme.surfaceVariant // 选中时的指示器颜色
    )
}
